package com.tanhua.admin.service;

import cn.hutool.core.date.DateUtil;
import com.itheima.tanhua.domain.Analysis;
import com.itheima.tanhua.vo.AnalysisSummaryVo;
import com.itheima.tanhua.vo.DashboardVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Lff
 * @date 2022/4/7
 */
@Service
public class DashboardService {

    @Autowired
    private AnalysisService analysisService;

    /**
     * 概要信息
     *
     * @return
     */
    public AnalysisSummaryVo summary() {
        //查询今天的统计数据

        Analysis today = analysisService.getAnalysisByDate(new SimpleDateFormat("yyyy-MM-dd").format(new Date()));


        //查询昨天的统计数据
        Analysis yesterday = analysisService.getAnalysisByDate(DateUtil.yesterday().toString("yyyy-MM-dd"));

        //今日新增用户涨跌率，单位百分数，正数为涨，负数为跌
        BigDecimal newUsersTodayRate = cal(today.getNumRegistered(), yesterday.getNumRegistered());
        //今日登录次数涨跌率，单位百分数，正数为涨，负数为跌
        BigDecimal loginTimesTodayRate = cal(today.getNumLogin(), yesterday.getNumLogin());
        //今日活跃用户涨跌率，单位百分数，正数为涨，负数为跌
        BigDecimal activeUsersTodayRate = cal(today.getNumActive(), yesterday.getNumActive());

        //累计用户数  累计注册用户
        Long cumulativeUsers = analysisService.countRegister(null, null);

        //过去30天用户活跃数
        String t1 = DateUtil.today();
        String month = DateUtil.lastMonth().toString("yyyy-MM-dd");
        Long activePassMonth = analysisService.countActiveByDate(month, t1);

        //过去七天用户活跃数
        String week = DateUtil.lastWeek().toString("yyyy-MM-dd");
        Long activePassWeek = analysisService.countActiveByDate(week, t1);

        //填充数据
        AnalysisSummaryVo analysisSummaryVo = new AnalysisSummaryVo();

        analysisSummaryVo.setCumulativeUsers(cumulativeUsers);//累计用户数
        analysisSummaryVo.setActivePassMonth(activePassMonth);//月活跃数
        analysisSummaryVo.setActivePassWeek(activePassWeek);//过去七天活跃数
        analysisSummaryVo.setNewUsersToday(Long.valueOf(today.getNumRegistered()));//今日新注册
        analysisSummaryVo.setNewUsersTodayRate(newUsersTodayRate);//今日新增用户涨跌率
        analysisSummaryVo.setLoginTimesToday(Long.valueOf(today.getNumLogin()));//今日登录数
        analysisSummaryVo.setLoginTimesTodayRate(loginTimesTodayRate);//今日登录涨跌率
        analysisSummaryVo.setActiveUsersToday(Long.valueOf(today.getNumActive()));//今日活跃数
        analysisSummaryVo.setActiveUsersTodayRate(activeUsersTodayRate);//今日活跃用户涨跌率

        return analysisSummaryVo;
    }

    /**
     * 计算各种百分值
     *
     * @param today
     * @param yesterday
     * @return 1005    2022-04-09	10	11	15	19	2022-04-10 14:28:15	2022-04-10 14:28:18
     * 1006	2022-04-10	111	113	152	191	2022-04-10 14:28:15	2022-04-10 14:28:18
     */
    private BigDecimal cal(Integer today, Integer yesterday) {
        BigDecimal result = null;
        try {
            BigDecimal t1 = new BigDecimal(today);
            BigDecimal t2 = new BigDecimal(yesterday);
            result = t1.subtract(t2).multiply(new BigDecimal(100)).divide(t2, RoundingMode.HALF_DOWN);
        } catch (Exception e) {
            e.printStackTrace();
            return new BigDecimal(0);
        }
        return result;
    }

    /**
     * 统计概要信息
     *
     * @param sd
     * @param ed
     * @param type
     * @return
     */
    public Map dashboard(Long sd, Long ed, String type) {
        //处理时间
        String startDate = DateUtil.date(sd).toString("yyyy-MM-dd");
        String endDate = DateUtil.date(ed).toString("yyyy-MM-dd");

        ///填充Vo
        //查询去年 2022-04-01
        DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd");

        String lastYearStart = LocalDate.parse(startDate, dtf).minusYears(1).format(dtf);//换算成去年开始时间
        String lastYearEnd = LocalDate.parse(endDate, dtf).minusYears(1).format(dtf);//换算成的结束时间

        //查询今年
        List<Analysis> thisYear = analysisService.findTimeScopeAnalysis(startDate, endDate);

        //查询去年
        //查询今年
        List<Analysis> lastYear = analysisService.findTimeScopeAnalysis(lastYearStart, lastYearEnd);

        //填充Vo
        DashboardVo[] thisYears = DashboardVo.init(thisYear, type);
        //封装到map
        DashboardVo[] lastYears = DashboardVo.init(lastYear, type);
        //返回
        Map resultMap = new HashMap();
        resultMap.put("thisYear", thisYears);
        resultMap.put("lastYear", lastYears);
        return resultMap;
    }
}
